package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class ConfigActivity extends AppCompatActivity {

    EditText dollarEditor;
    EditText euroEditor;
    EditText wonEditor;


    private static final String TAG = "ConfigActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config);

        //接收数据
        Intent intent = getIntent();
        float dollar2 = intent.getFloatExtra("key_dollar",0.0f);           //后面的值为默认值，当前者不存在时则使用默认值
        float euro2 = intent.getFloatExtra("key_euro",0.0f);
        float won2 = intent.getFloatExtra("key_won",0.0f);

        Log.i(TAG, "onCreate:dollar2=" + dollar2);
        Log.i(TAG, "onCreate:euro2=" + euro2);
        Log.i(TAG, "onCreate:won2=" + won2);

        //获取控件对象
        dollarEditor = findViewById(R.id.input_newDollar);
        euroEditor = findViewById(R.id.input_newEuro);
        wonEditor = findViewById(R.id.input_newWon);

        //放入控件中
        dollarEditor.setText(String.valueOf(dollar2));
        euroEditor.setText(String.valueOf(euro2));
        wonEditor.setText(String.valueOf(won2));

    }

    public void save(View btn){
        Log.i(TAG, "save: ");
        //获取用户新输入的数据项
        float newdollar = Float.parseFloat(dollarEditor.getText().toString());
        float neweuro = Float.parseFloat(euroEditor.getText().toString());
        float newwon = Float.parseFloat(wonEditor.getText().toString());

        Log.i(TAG, "save: newdollar = " + newdollar);
        Log.i(TAG, "save: neweuro = " + neweuro);
        Log.i(TAG, "save: newwon = " + newwon);

        //放入intent中
        Intent ret = getIntent();
        Bundle bdl = new Bundle();
        bdl.putFloat("new_dollar",newdollar);
        bdl.putFloat("new_euro",neweuro);
        bdl.putFloat("new_won",newwon);
        ret.putExtras(bdl);

        setResult(1,ret);
        //返回到调用窗口
        finish();


    }
}